﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;
using System.ServiceModel;
using BlogEngine.Core;
using System.Globalization;

namespace AzureProvider.SoupMethods
{
    public class PagesMethods :BaseMethods 
    {        
        public IEnumerable<Page> GetPages(string query, out string errMessage)
        {
            return base.GetObjects(query, out errMessage).Cast<Page>();
        }        

        protected override string containerId
        {
            get { return "Pages"; }
        }

        protected override string kind
        {
            get { return "Page"; }
        }

        protected override Entity CreateEntity(object Object)
        {
            Page  page = (Page)Object;

            var entity = new Entity()
                {
                    Id = page.Id.ToString(),
                    Kind = kind
                };

                entity.Properties = new Dictionary<string, object>();
                entity.Properties.Add("Title", page.Title);
                entity.Properties.Add("Description", page.Description);
                entity.Properties.Add("Content", page.Content);
                entity.Properties.Add("Keywords", page.Keywords);
                entity.Properties.Add("Slug", page.Slug);
                entity.Properties.Add("Parent", page.Parent.ToString());
                entity.Properties.Add("IsFrontPage", page.IsFrontPage);
                entity.Properties.Add("ShowInList", page.ShowInList);
                entity.Properties.Add("IsPublished", page.IsPublished);
                
                entity.Properties.Add("DateCreated", 
                    page.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss",
                    CultureInfo.InvariantCulture));
                entity.Properties.Add("DateModified", 
                    page.DateModified.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", 
                    CultureInfo.InvariantCulture));
              
            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            Page page = new Page
             {
                 Id = new Guid(Entity.Id),
                 Title = (string)Entity.Properties.Single(tt => tt.Key == "Title").Value,
                 Description = (string)Entity.Properties.Single(tt => tt.Key == "Description").Value,
                 Content = (string)Entity.Properties.Single(tt => tt.Key == "Content").Value,
                 Keywords = (string)Entity.Properties.Single(tt => tt.Key == "Keywords").Value,
                 Slug = (string)Entity.Properties.Single(tt => tt.Key == "Slug").Value,
                 Parent = new Guid((string)Entity.Properties.Single(tt => tt.Key == "Parent").Value),

                 IsFrontPage = (bool)Entity.Properties.Single(tt => tt.Key == "IsFrontPage").Value,
                 ShowInList = (bool)Entity.Properties.Single(tt => tt.Key == "ShowInList").Value,
                 IsPublished = (bool)Entity.Properties.Single(tt => tt.Key == "IsPublished").Value,

                 DateCreated = DateTime.Parse((string)Entity.Properties.Single(tt => tt.Key == "DateCreated").Value),
                 DateModified = DateTime.Parse((string)Entity.Properties.Single(tt => tt.Key == "DateModified").Value), 

             };

            return page;
        }
    }
}
